﻿using FNN.Helpers;
using FNN.NetworkMasters;
using FNN.Networks;
using FNN.SourcePoints;
using Microsoft.Win32;
using System.Collections.Generic;
using System.Windows;

namespace FNNClientApplication
{
    /// <summary>
    /// Логика взаимодействия для MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public SerializationHelper serializationHelper = new SerializationHelper();

        public MainWindow()
        {
            InitializeComponent();
        }

        protected List<DateSourcePoint> GetFinancialDataSource(string pathToData)
        {
            var csvHelper = new FinancialCsvHelper(pathToData);
            var initialData = csvHelper.GetBody();
            var dataHeaders = csvHelper.GetHeader();
            var tempList = csvHelper.ProcessData(dataHeaders, initialData);
            List<DateSourcePoint> result = new List<DateSourcePoint>();

            foreach (var point in tempList)
            {
                result.Add((DateSourcePoint)point);
            }

            return result;
        }

        protected List<SourcePoint> GetDataSource(string pathToData, NetworkMastersList networkType)
        {
            CsvHelper csvHelper = null;

            switch (networkType)
            {
                case NetworkMastersList.Financial:
                    csvHelper = new FinancialCsvHelper(pathToData);
                    break;
                case NetworkMastersList.Regular:
                    csvHelper = new RegularCsvHelper(pathToData);
                    break;
            }

            if (csvHelper == null) return null;

            var initialData = csvHelper.GetBody();
            var dataHeaders = csvHelper.GetHeader();
            var result = csvHelper.ProcessData(dataHeaders, initialData);

            return result;
        }
        
        private void BtnChooseData_Click(object sender, RoutedEventArgs e)
        {
            var fileBrowser = new OpenFileDialog()
            {
                CheckFileExists = true,
                Multiselect = false,
                Filter = "CSV|*.csv"
            };
            fileBrowser.ShowDialog();
            TxtDataPath.Text = fileBrowser.FileName;
        }

        private void BtnChooseNetwork_Click(object sender, RoutedEventArgs e)
        {
            var fileBrowser = new OpenFileDialog()
            {
                CheckFileExists = true,
                Multiselect = false,
                Filter = "FNN|*.fnn|XML|*.xml|SOAP|*.soap"
            };
            fileBrowser.ShowDialog();
            TxtNetworkPath.Text = fileBrowser.FileName;
        }

        private void BtnStart_Click(object sender, RoutedEventArgs e)
        {
            var targetNetwork = serializationHelper.LoadBinary(TxtNetworkPath.Text);
            if (targetNetwork is FinancialNetwork)
            {
                var networkMaster = new FinancialNetworkMaster();
                networkMaster.DataSource = GetDataSource(TxtDataPath.Text, NetworkMastersList.Financial);
                networkMaster.RunEpoch(ref targetNetwork, (targetNetwork.OutputCount > 0));
                var activityWindow = new NetworkActivityWindow((FinancialNetwork)targetNetwork, GetFinancialDataSource(TxtDataPath.Text));
                activityWindow.Show();
            }
            else if (targetNetwork is RegularNetwork)
            {
                var networkMaster = new RegularNetworkMaster();
                networkMaster.DataSource = GetDataSource(TxtDataPath.Text, NetworkMastersList.Regular);
                networkMaster.RunEpoch(ref targetNetwork, (targetNetwork.OutputCount > 0));
                var resultsWindow = new ResultsReviewWindow();
                int successCount = ((RegularNetwork)targetNetwork).SuccessCount;
                int fullCount = networkMaster.DataSource.Count;
                resultsWindow.ShowNetworkResults(successCount, fullCount);
                resultsWindow.Show();
            }
        }
    }
}
